package com.xingdata.jjxc.activity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.adapter.CarBrandListViewAdapter;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.enty.AddCarEntity;
import com.xingdata.jjxc.enty.CarBrandEntity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.http.HttpResponseListener;

/**
 * 汽车品牌 车型列表
 * 
 * @author Administrator
 * 
 */
public class CarBrandListViewActivity extends Base1Activity {
	private String TAG = "CarBrandListViewActivity";
	private ExpandableListView listView;
	private TextView title;
	private AddCarEntity addCarEntity = new AddCarEntity();
	private CarBrandListViewAdapter adapter;
	private List<CarBrandEntity> brandEntities = new ArrayList<CarBrandEntity>();
	private CarBrandEntity brandEntity = new CarBrandEntity();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initData();

	}

	private void initData() {
//		brandEntities.addAll((Collection<? extends CarBrandEntity>) getIntent().getExtras().getParcelableArrayList(
//				"brand"));
//		adapter.notifyDataSetChanged();
	}

	@Override
	protected int getContentViewId() {
		return R.layout.carbrand_listview;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return "品牌列表";
	}

	@Override
	protected void initViews() {
		listView = (ExpandableListView) findViewById(R.id.expandableListView);
		adapter = new CarBrandListViewAdapter(CarBrandListViewActivity.this, brandEntities);
		listView.setAdapter(adapter);
		listView.setOnGroupExpandListener(new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int arg0) {
				for (int i = 0; i < brandEntities.size(); i++) {
					if (arg0 != i) {
						listView.collapseGroup(i);

					}
				}
			}
		});

		listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
				CarBrandEntity entity = new CarBrandEntity();
				
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				entity.setCar_branch_id(brandEntities.get(groupPosition).getSub_branch().get(childPosition).getCar_branch_id());
				entity.setCar_branch_name(brandEntities.get(groupPosition).getSub_branch().get(childPosition).getCar_branch_name());
				bundle.putSerializable("entity", entity);
				intent.putExtras(bundle);
				
				setResult(RESULT_OK, intent);
				
				finish();
				return true;
			}
		});

		listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

				if (brandEntities.get(groupPosition).getSub_branch() == null
						|| brandEntities.get(groupPosition).getSub_branch().size() == 0) {
					CarBrandEntity entity = new CarBrandEntity();

					Intent intent = new Intent();
					Bundle bundle = new Bundle();
					entity.setCar_branch_id(brandEntities.get(groupPosition).getCar_branch_id());
					entity.setCar_branch_name(brandEntities.get(groupPosition).getCar_branch_name());
					bundle.putSerializable("entity", entity);
					intent.putExtras(bundle);
					
					setResult(RESULT_OK, intent);
					finish();
				}
				Log.i(TAG, brandEntities.get(groupPosition).getCar_branch_name());
				return false;
			}
		});
	}

	/**
	 * 获取车辆品牌信息
	 */
	private void doPost_getcarbranchlist() {
		paramsMap = new HashMap<String, String>();
		paramsMap.put("pakege_name", this.getPackageName());
		mHttpUtil.Post(App.ZZD_REQUEST_GETCARBRANCHLIST, paramsMap, new HttpResponseListener() {

			@Override
			public void onSuccess(RespEntity respEntity) {
				brandEntities.addAll(JSON.parseArray(respEntity.getResult(), CarBrandEntity.class));
				adapter.notifyDataSetChanged();

			}

			@Override
			public void onLoading(int vurrent) {

			}

			@Override
			public void onFailure(String result) {

			}
		});
	}

}
